<template>
     <div class="components-container">
         <div class="header-form">
            <div class="layui-inline">
                <label class="layui-form-label">厂家名称</label>
                <div class="layui-input-inline">
                    <el-select v-model="selectData.VDRIDEQUALS" placeholder="请选择厂家" @change="btnSearch">
                     <el-option v-for="item in Manufactor" :key="item.VDRID" :label="item.VDRNAME" :value="item.VDRID"/>
                    </el-select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">物品类别</label>
                <div class="layui-input-inline">
                    <el-select v-model="selectData.CYPIDEQUALS" placeholder="请选择物品类别" @change="btnSearch">
                     <el-option label="全部" :value="null"></el-option>
                     <el-option v-for="item in chargeProjectTypes" :key="item.value" :label="item.label" :value="item.value"/>
                    </el-select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" >统计日期</label>
                <el-date-picker v-date v-model="selectData.STARBAECHECKDATE" type="date" placeholder="选择日期范围" @change="btnSearch">
                </el-date-picker>
                <el-date-picker v-date v-model="selectData.ENDBAECHECKDATE" type="date" placeholder="选择日期范围" @change="btnSearch">
                </el-date-picker>
            </div>
            <el-button type="primary" :disabled="downloadLoading" @click="btnSearch" :loading="this.$store.state.isloading" >搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div style="margin-top:10px;">
          <ul class="toolList"> 
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :disabled="$store.state.isloading" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" :disabled="this.$store.state.isloading"  @click="exportExcel" style="margin-left:20px;margin-bottom:-10px;">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :isIndex="true" :hiddenColumn="false" :pageOrder="pageOrder" :hiddenPageOrder="false" :height="height" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
   
        <printmodal ref="printPdf"></printmodal>
        <cemeteryData ref="simpleReg" :orderType="order" @refreshData="getData"></cemeteryData>
     </div>
</template>


<script>
import dataTable from "@/components/Table/DataTable";
import cemeteryData from "./tombsUseMain";

import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import printmodal from "@/components/Pdf/Print";
import { PRINT } from "@/components/Pdf/PDF.js";
const INFO = "详情";
const EXPORT = "导出";
const ADD = '新增';
export default {
  name: "stockReport",
  mixins: [manage],
  data() {
    return {
      selectData: {
        STARBAECHECKDATE: new Date(new Date().getFullYear(),new Date().getMonth(),1),  
        ENDBAECHECKDATE: new Date(), 
        VDRIDEQUALS: null,  
        CYPIDEQUALS: null,  
      },
      height: "620",
      downloadLoading:false,
      Manufactor: [],
      cdhTypeDatas: [],
      IsDPGPushCHA: null, //义乌下拉个性化参数
      chargeProjectTypes: [], //物品类别
    };
  },
  methods: {
      //获取收费项目类别
      getChargeProjectType() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().ChargeProject.QueryType,
          res => {
            let cdhTypeData = res.data.content;
            this.cdhTypeDatas = res.data.content;
            if(this.IsDPGPushCHA){ //义乌
              let datas = [];
              cdhTypeData.forEach(item => {
                if(item.CYPTYPE === "A"){
                  datas.push(item);
                }
              });
              this.chargeProjectTypes = datas.map(item => ({
                label: item.CYPNAME,
                value: item.CYPID
              }))
            }else{
              let legendsCdhData = cdhTypeData.map(item => ({    
                label: item.CYPNAME,
                value: item.CYPID
              }));
              this.chargeProjectTypes = legendsCdhData;
            }
          },
          res => {
            this.$message.error(res.data.msg);
          }
        );
      },
    exportExcel() {
      var startTime;
      var endTime;
      if(this.selectData.STARBAECHECKDATE){
        startTime = this.selectData.STARBAECHECKDATE.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.ENDBAECHECKDATE){
        endTime = this.selectData.ENDBAECHECKDATE.toLocaleDateString();
      }else{
        endTime = '空';
      }
      var header = '库存汇总表' + '(' + startTime + '--' + endTime + ')';
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.dataList, header);
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case INFO:
        //   this.$refs.simpleReg.open(this.order, this.currentRow.TMMID);
          break;
        case EXPORT:
          // this.$refs.printPdf.report(PRINT.PDFICEDISPATCHSEARCH,this.getDataC());
          break;
        default:
      }
    },
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetStockColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
      return Data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      if(this.selectData.STARBAECHECKDATE){
        Data.STARBAECHECKDATE =  formatDate(Data.STARBAECHECKDATE, "yyyyMMdd");
      }
      if(this.selectData.ENDBAECHECKDATE){
        Data.ENDBAECHECKDATE =  formatDate(Data.ENDBAECHECKDATE, "yyyyMMdd");
      }
      apiBase.DoService(Data, apiBase.getApiAdress().Report.StockSummary, res => {
        this.dataList = res.data.content.OutData;
        this.pageOrder.totalNum = res.data.content.RecordCount;
      });
    },
    //获取所属厂家
    QueryVdr(){
      apiBase.DoService(
        {},
        apiBase.getApiAdress().Depot.QueryVdrByZC,
        res => {
            this.Manufactor = res.data.content.OutData;
        }
      )
    },

    btnShow(btn){
      switch (btn) {
          case '导出':
            return true;
            break;
          case '详情':
            return true;
            break;
          default: return false;
        }
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {
        STARBAECHECKDATE: new Date(new Date().getFullYear(),new Date().getMonth(),1),  
        ENDBAECHECKDATE: new Date(), 
        VDRIDEQUALS: null,  
        CYPIDEQUALS: null,  
      };
      this.getData();
    }
  },
  mounted() {
  },
  components: {
    dataTable,
    cemeteryData,
    printmodal
  },
  created() {
    let data = {};
    apiBase.DoService(data, apiBase.getApiAdress().Param.GetIsDPGPushCHA, res => {
      this.IsDPGPushCHA = res.data.content;
    });
    this.getColumns();//获取headerlist
    this.getData();//获取数据
    this.QueryVdr(); //获取厂家下拉
    this.getChargeProjectType(); //获取收费项目类别
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>

